import jwtdecode from 'jwt-decode';
// 存储方式
const storage = 'localStorage';
// token的key
const tokenKey = 'token';

// 获取accessToken
export function getToken() {
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.getItem(tokenKey);
    } else if ('sessionStorage' === storage) {
      return sessionStorage.getItem(tokenKey);
    } else {
      return localStorage.getItem(tokenKey);
    }
  } else {
    return localStorage.getItem(tokenKey);
  }
}
// 存储accessToken
export function setToken(accessToken) {
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.setItem(tokenKey, accessToken);
    } else if ('sessionStorage' === storage) {
      return sessionStorage.setItem(tokenKey, accessToken);
    } else {
      return localStorage.setItem(tokenKey, accessToken);
    }
  } else {
    return localStorage.setItem(tokenKey, accessToken);
  }
}
// 移除accessToken
export function removeToken() {
  if (storage) {
    if ('localStorage' === storage) {
      return localStorage.removeItem(tokenKey);
    } else if ('sessionStorage' === storage) {
      return sessionStorage.clear();
    } else {
      return localStorage.removeItem(tokenKey);
    }
  } else {
    return localStorage.removeItem(tokenKey);
  }
}

// token 解密
export function getTokenInfo(){
  return jwtdecode(localStorage.getItem(tokenKey)) ?? ''
}
